home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '88 / Proceedings '88 / Feldt Advanced Mac Programming / Serial Port / lib src / XTWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1987-10-26  |  4.6 KB  |  177 lines  |  [TEXT/KAHL]

  1. /*                         Commlib - XTWindow                                */
  2. /*                          Aztec C compiler 1.06i                            */
  3. /*                        Lightspeed C compiler 2.01                        */
  4.  
  5.  
  6. /*        Copyright © 1985,1986 by small systems guild.  All rights reserved.    */
  7.  
  8. #include  <extender.h>   /* include Extender and standard Toolbox headers    */
  9.  
  10. WindowPtr CreateWindow(wPtr,R,Title,wType,Visible,GoAwayBox,GrowBox,vScrollBar,hScrollBar)
  11. WindowRecord    *wPtr;
  12. Rect            *R;
  13. char            *Title;
  14. int                wType;
  15. Boolean            Visible,GoAwayBox,GrowBox,vScrollBar,hScrollBar;
  16. {
  17.     WData        WD;
  18.     WDHandle     tempWDH;
  19.  
  20.     if ((wType < 0) || (wType > 23) ||
  21.             ((wType > 4) && (wType < 16) && (wType != 8) && (wType != 12)))
  22.         return(NULL);                /* refuse unkown window type parameter    */
  23.  
  24.     InitWData(&WD);
  25.     wPtr = (WindowRecord *)NewWindow(wPtr,R,Title,FALSE,wType,NEG_ONE,GoAwayBox,NULL);
  26.     if (!ValidPointer((Ptr)wPtr))
  27.         return(NULL);
  28.  
  29.     tempWDH = (WDHandle)NewHandle((long)sizeof(WData));
  30.     SetWRefCon(wPtr,tempWDH);
  31.     if (!ValidHandle((Handle)tempWDH))
  32.         return(NULL);
  33.  
  34.     (**tempWDH).sync1 = SYNC1VAL;
  35.     (**tempWDH).sync2 = SYNC2VAL;
  36.     SetWData((WindowPtr)wPtr,&WD);
  37.  
  38.     if (!Visible)
  39.         return((WindowPtr)wPtr);
  40.  
  41.     ShowWindow(wPtr);
  42.     SetPort(wPtr);
  43.     SelectWindow(wPtr);
  44.  
  45.     return((WindowPtr)wPtr);
  46. }
  47.  
  48. void InitWData(wDataPtr)            /* initializes WData record to blank values    */
  49. WData   *wDataPtr;
  50. {
  51.     if (ValidPointer((Ptr)wDataPtr)) {
  52.         wDataPtr->windowPic = NULL;
  53.         wDataPtr->TEH = NULL;
  54.         wDataPtr->wList = NULL;
  55.         wDataPtr->vScrollBar = NULL;
  56.         wDataPtr->hScrollBar = NULL;
  57.         wDataPtr->GrowBox = FALSE;
  58.         wDataPtr->exceptFlags = NULL;
  59.         wDataPtr->sync1 = NULL;
  60.         wDataPtr->sync2 = NULL;
  61.         wDataPtr->rsrv1 = NULL;
  62.         wDataPtr->rsrv2 = NULL;
  63.         wDataPtr->rsrv3 = NULL;
  64.         wDataPtr->rsrv4 = NULL;
  65.         wDataPtr->UsrRef1 = NULL;
  66.         wDataPtr->UsrRef2 = NULL;
  67.     }
  68. }
  69.  
  70. void GetWData(wPtr,wDataPtr)    /* copies WData struct for given window pointer */
  71. WindowPtr   wPtr;
  72. WData       *wDataPtr;
  73. {
  74.     WDHandle    WDtemp;
  75.     int            wType;
  76.  
  77.   if (ValidPointer((Ptr)wDataPtr)) {
  78.     if (ValidPointer((Ptr)wPtr)) {
  79.         wType = ((WindowPeek)wPtr)->windowKind;
  80.         if ((wType >= 0) && (wType != dialogKind)) {/* not DA or dialog wind*/            
  81.             WDtemp = (WDHandle)GetWRefCon(wPtr);
  82.             if (ValidHandle((Handle)WDtemp))        /* if the handle looks OK         */
  83.                 if (ValidWData(*WDtemp)) {    /* and the WData record is valid*/
  84.                     *wDataPtr = **WDtemp;    /* copy into user's WData struct*/
  85.                     RETURN;
  86.                 }
  87.         }
  88.     }
  89.     InitWData(wDataPtr);                /* zero out all fields of WDataRecord    */
  90.   }
  91. }
  92.  
  93. void SetWData(wPtr,wDataPtr)        /* sets WData struct for window pointer */
  94. WindowPtr   wPtr;
  95. WData       *wDataPtr;
  96. {
  97.     WDHandle    WDtemp;
  98.     int            wType;
  99.  
  100.     if (ValidPointer((Ptr)wPtr) && ValidPointer((Ptr)wDataPtr)) {
  101.         wType = ((WindowPeek)wPtr)->windowKind;
  102.         if ((wType >= 0) && (wType != dialogKind)) {/* not DA or dlog wind    */            
  103.             WDtemp = (WDHandle)GetWRefCon(wPtr);
  104.             if (ValidHandle((Handle)WDtemp))                /* if handle looks OK     */
  105.                 if (ValidWData(*WDtemp)) {            /* and WData rec valid     */
  106.                     wDataPtr->sync1 = SYNC1VAL;        /* set sync fields        */ 
  107.                     wDataPtr->sync2 = SYNC2VAL;
  108.                     **WDtemp = *wDataPtr;            /* copy user's WData     */
  109.                     RETURN;
  110.                 }
  111.         }
  112.     }
  113. }
  114.  
  115. void KillWData(wPtr)        /* disposes of a window's WData structure */
  116. WindowPtr    wPtr;
  117. {
  118.     WData    WD;
  119.  
  120.     if (ValidPointer((Ptr)wPtr)) {
  121.         GetWData(wPtr,&WD);
  122.         if (ValidWData(&WD)) {
  123.             if (ValidHandle((Handle)(WD.windowPic)))
  124.                 DisposHandle((Handle)WD.windowPic);            
  125.             DisposHandle((Handle)GetWRefCon(wPtr));
  126.             SetWRefCon(wPtr,NULL);
  127.         }
  128.     }
  129. }
  130.  
  131. void SetWPic(wPtr,thePic)
  132. WindowPtr   wPtr;
  133. PicHandle   thePic;
  134. {
  135.     WData    WD;
  136.     Rect    R;
  137.  
  138.     GetWData(wPtr,&WD);
  139.     R = (**thePic).picFrame;
  140.     if (WD.hScrollBar != NULL) {
  141.         SetCtlMax(WD.hScrollBar,R.right);
  142.         SetCtlMin(WD.hScrollBar,R.left);
  143.         SetCtlValue(WD.hScrollBar,R.left);
  144.     }
  145.     if (WD.vScrollBar != NULL) {
  146.         SetCtlMax(WD.vScrollBar,R.bottom);
  147.         SetCtlMin(WD.vScrollBar,R.top);
  148.         SetCtlValue(WD.vScrollBar,R.top);
  149.     }
  150.     WD.windowPic = thePic;
  151.     SetWData(wPtr,&WD);
  152.     InvalRect(&(wPtr->portRect));    /* causes window content to be updated  */
  153. }
  154.  
  155. void KillWindow(wPtr)     /* disposes of WData, controls and closes window     */
  156. WindowPtr   wPtr;
  157. {
  158.     WData       WD;
  159.     
  160.     KillWData(wPtr);
  161.     KillControls(wPtr);
  162.     CloseWindow(wPtr);
  163. }
  164.  
  165. Boolean ValidWData(WDptr)
  166. WData    *WDptr;
  167. {
  168.     if (ValidPointer((Ptr)WDptr)) {                /* if pointer is potentially valid    */
  169.         if ((WDptr->sync1 == SYNC1VAL) &&    /* and sync values match        */
  170.                 (WDptr->sync2 == SYNC2VAL))
  171.             return(TRUE);                    /* then WData record is valid        */
  172.     }
  173.  
  174.     return(FALSE);                            /* else the WData pointer was bad    */
  175. }
  176.  
  177.